package com.sharker.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.ToastUtils;
import com.sharker.R;
import com.sharker.application.App;
import com.sharker.models.data.SessionModel;
import com.sharker.network.SharkerParams;
import com.sharker.utils.RegexUtils;
import com.sharker.view.Toptitlebar;

import org.xutils.common.Callback;
import org.xutils.x;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private Button mBtn;
    private TextView mMessage;
    private EditText login_account;
    private EditText login_password;
    private TextView forgetpassword;
    private Button login;
    private Button regist;
    private RelativeLayout wechat;
    private RelativeLayout qq;
    private Toptitlebar mTitlebar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        mTitlebar = (Toptitlebar) findViewById(R.id.login_titlebar);
        login_account = (EditText) findViewById(R.id.login_account);
        login_password = (EditText) findViewById(R.id.login_password);
        forgetpassword = (TextView) findViewById(R.id.forgetpassword);
        forgetpassword.setOnClickListener(this);
        login = (Button) findViewById(R.id.login);
        login.setOnClickListener(this);
        regist = (Button) findViewById(R.id.regist);
        regist.setOnClickListener(this);
        wechat = (RelativeLayout) findViewById(R.id.wechat);
        wechat.setOnClickListener(this);
        qq = (RelativeLayout) findViewById(R.id.qq);
        qq.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login://登录
                submit();
                break;
            case R.id.regist://快速注册
                startActivity(new Intent(this, RegisterActivity.class));
                break;
            case R.id.qq://第三方qq登录

                break;
            case R.id.wechat://第三方微信登录

                break;
            case R.id.forgetpassword://忘记密码
                startActivity(new Intent(this, ResetPasswordActivity.class));
                break;
        }
    }

    private void submit() {
        // validate
        String account = login_account.getText().toString().trim();
        if (TextUtils.isEmpty(account) || !RegexUtils.isMobileExact(account)) {
            Toast.makeText(this, "手机号或密码错误", Toast.LENGTH_SHORT).show();
            return;
        }

        String password = login_password.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "手机号或密码错误", Toast.LENGTH_SHORT).show();
            return;
        }
        // TODO validate success, do something
        App.sBoolean = true;
        SharkerParams sharkerParams = new SharkerParams("user_login");
        // sharkerParams.addBodyParameter("number", account);
        x.http().post(sharkerParams, new Callback.CommonCallback<SessionModel>() {
            @Override
            public void onSuccess(SessionModel result) {
                ToastUtils.showShortToast(result.data.toString());
                Log.i("TAG", "点击登录后" + result.toString());
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
